
$(function () {
    if (editType != 'info') {
        regValidate(["#_menuDataForm"]);
        initTextSearch("#parentMenuName",
            basePath + 'pfMenu/getParentMenuData?menuName=',
            ['id', 'menuName'],
            {'id':'菜单ID', 'menuName':'菜单名称'},
            'id',
            'menuName',
            true,
            selectMenuValue,
            noSelectMenuValue);
    }
});

function doMenuLevelChange() {
    var menuLevel = $('#menuLevel').val();
    if (menuLevel == 1) {
        $('#_parentMenu').css('display', 'none');
    } else {
        $('#_parentMenu').css('display', 'block');
    }
}

function doMenuSave() {
    if(!$("#_menuDataForm").valid()) return;//校验不通过
    var parentId = $('#parentId').val();
    var menuName = $('#menuName').val();
    var menuUrl = $('#menuUrl').val();
    var menuLevel = $('#menuLevel').val();
    var sortNum = $('#sortNum').val();
    var menuImg = $('#menuImg').val();
    var remark = $('#remark').val();
    if (!sortNum || sortNum == '') {
        sortNum = 999;
    }
    if (menuLevel == 1&&!parentId) {
        parentId=0;
    }
    var postData = {
        parentId : parentId,
        menuName : menuName,
        menuUrl : menuUrl,
        menuLevel : menuLevel,
        menuImg : menuImg,
        sortNum : sortNum,
        remark : remark
    };
    var url = basePath + 'pfMenu/addPfMenu';
    if (editType == 'edit') {
        postData.id = oneMenuInfo.id;
        url = basePath + 'pfMenu/editPfMenu';
    }
    $.ajax({
        type: 'post',
        url: url,
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if (!data) {
                toastr.error(common_save_fail);
                return;
            }
            if (data.isSuccess) {
                toastr.success(common_save_success);
                goBack();
                return;
            }
            if (!data.isSuccess && data.errorCode == 'EXIST') {
                toastr.warning('【'+menuName+'】已存在');
                return;
            }
            if (!data.isSuccess) {
                toastr.error(data.errorDesc);
                return;
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
            return;
        }
    });
}

function doBack() {
    if (editType != 'info' &&!checkDataChange("#_menuDataForm",oneMenuInfo)) {  //判断图片
        swal({
            title:"提示",
            text:common_no_save_msg,
            showCancelButton:true,
            showConfirmButton:true,
            confirmButtonText:"确定",
            cancelButtonText:"取消",
            animation:"slide-from-top"
        }, function() {
            goBack();
        });
        return;
    }
    goBack();
}

function goBack() {
    var str = '&menuName=' + searchInfo.menuName + '&status=' + searchInfo.status + (!searchInfo.menuLevel ? '' : '&menuLevel=' + searchInfo.menuLevel)
        + '&pageNo=' + searchInfo.pageNo + '&pageSize=' + searchInfo.pageSize;
    window.location.href = basePath + 'pfMenu/page?t=' + new Date().getTime() + str;
}

function selectMenuValue(data) {
    if (!data || !data.id) {
        return;
    }
    $('#parentId').val(data.id);
}

function noSelectMenuValue() {
    $('#parentId').val('');
}